package com.pavelk.orion.controller
{
	import flash.display.DisplayObject;
	
	import org.puremvc.as3.interfaces.ICommand;
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.command.SimpleCommand;
    
    import org.puremvc.as3.utilities.loadup.model.LoadupResourceProxy;
    import org.puremvc.as3.utilities.loadup.model.LoadupMonitorProxy;
    import org.puremvc.as3.utilities.loadup.interfaces.ILoadupProxy;
    
    import com.pavelk.orion.ApplicationFacade;

	import com.pavelk.orion.view.ApplicationMediator;
	
	import com.pavelk.orion.model.SiteDataProxy;
	import com.pavelk.orion.model.RuntimeAssetsProxy;
	import com.pavelk.orion.model.RuntimeFontsProxy;
	
	public class StartupCommand extends SimpleCommand implements ICommand
	{
		private var monitor:LoadupMonitorProxy;
		
		override public function execute( note:INotification ) : void    
        {
	    	//mediator
	    	facade.registerMediator( new ApplicationMediator( note.getBody().st as DisplayObject ) );
			
			//loadup monitor
			facade.registerProxy( new LoadupMonitorProxy() );
			monitor = facade.retrieveProxy( LoadupMonitorProxy.NAME ) as LoadupMonitorProxy;
			//monitor.defaultRetryPolicy = 
			
			//proxies
			var siteDataProxy     :ILoadupProxy = new SiteDataProxy();
			var runtimeAssetsProxy:ILoadupProxy = new RuntimeAssetsProxy();
			var runtimeFontsProxy :ILoadupProxy = new RuntimeFontsProxy();
			
			facade.registerProxy( siteDataProxy );
			facade.registerProxy( runtimeAssetsProxy );
			facade.registerProxy( runtimeFontsProxy );
			
			//resource twins for proxies, only for loadup utility
			var rSiteDataProxy     :LoadupResourceProxy = makeAndRegisterLoadupResource( SiteDataProxy.SRNAME, siteDataProxy );
			var rRuntimeAssetsProxy:LoadupResourceProxy = makeAndRegisterLoadupResource( RuntimeAssetsProxy.SRNAME, runtimeAssetsProxy );
			var rRuntimeFontsProxy :LoadupResourceProxy = makeAndRegisterLoadupResource( RuntimeFontsProxy.SRNAME, runtimeFontsProxy );
			
			//load sequence
			//rSiteDataProxy.requires = [ rRuntimeFontsProxy, rRuntimeAssetsProxy ];
			rSiteDataProxy.requires = [ rRuntimeAssetsProxy, rRuntimeFontsProxy ];
			rRuntimeFontsProxy.requires = [ rRuntimeAssetsProxy ];
			//rRuntimeAssetsProxy.requires = [ rSiteDataProxy ];
			
			monitor.loadResources();
        }
        
        //helper method for register resource proxies
        private function makeAndRegisterLoadupResource( proxyName:String, appResourceProxy:ILoadupProxy ) : LoadupResourceProxy 
		{
            var r:LoadupResourceProxy = new LoadupResourceProxy( proxyName, appResourceProxy );
            facade.registerProxy( r );
            monitor.addResource( r );
            return r;
        }
	}
}